'use strict';

const Service = require('egg').Service;

const { ME_CATCE_KEY, WORK_LIST_CATCH_KEY } = require('../../config/redisKey');

class SkillsService extends Service {

  setDate(data) {
    const { startTime, endTime } = data
    if (startTime, endTime) {
      data.startTime = new Date(startTime)
      data.endTime = new Date(endTime)
    }
  }

  /**
   * 创建
   */
  async create(data) {
    await this.ctx.clearCache(ME_CATCE_KEY, WORK_LIST_CATCH_KEY)
    this.setDate(data)
    const { Me } = this.ctx.model;
    await Me.addWork(data);
  }

  /**
   * 更新数据
   */
  async update(id, data) {
    await this.ctx.clearCache(ME_CATCE_KEY, WORK_LIST_CATCH_KEY)
    this.setDate(data)
    const { Me } = this.ctx.model;
    return await Me.updateWork(id, data);
  }

  /**
   * 删除数据
   */
  async destroy(id) {
    await this.ctx.clearCache(ME_CATCE_KEY, WORK_LIST_CATCH_KEY)
    const { Me } = this.ctx.model;
    return await Me.removeWork(id);
  }

  /**
   * 获取列表
   */
  async list() {
    return this.ctx.getCache(WORK_LIST_CATCH_KEY, async () => {
      const { Me } = this.ctx.model;
      const meModel = await Me.findOne();
      return meModel.works
    })
  }
}

module.exports = SkillsService;
